<%@ page language="java" import="java.util.*,com.hs.auth.model.*,com.hs.dnms.model.*,com.hs.core.cache.*" pageEncoding="utf-8"%>
 <%
  String batchid = (String)request.getAttribute("batchid");
  DnmsBook obj = (DnmsBook)request.getAttribute("obj");
  DnmsDisease disobj = (DnmsDisease)request.getAttribute("disobj"); 
  
  List prelist = new ArrayList();
  List herblist = new ArrayList();
  StringBuffer symstr= new StringBuffer();
  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="../../include.jsp"%>  
<script language="javascript" type="text/javascript" src="<%=basePath%>common/uploadify/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript" src="<%=basePath%>common/javascripts/My97DatePicker/WdatePicker.js"></script>
<script language="javascript" type="text/javascript" src="<%=basePath%>common/uploadify/jquery.uploadify.v2.1.4.js"></script>
<script type="text/javascript" src="<%=basePath%>common/uploadify/swfobject.js"></script>
<LINK rel=stylesheet type=text/css href="<%=basePath%>common/uploadify/uploadify.css"/>  
<script src='<%=basePath%>dwr/interface/dataProhelper.js'></script>  

<script language="javascript" type="text/javascript">
function dataPro(id){
	window.location.href="";

}
var disid = "<%=disobj ==null?"":disobj.getId()%>";
/**方剂信息保存*/
var symid = "";
function savePreinfo(){
	var symid = getRadioValue("sym");
	if("undefined" == typeof(symid)){
		alert("请先选择该方剂相关的证候！");
		return;
	} 
 	var paraname = document.getElementById("prename").value; 
 	var laiyf = document.getElementById("laiyf").value;  
 	var jixing = document.getElementById("jixing").value;  
 	var eatemethod = document.getElementById("eatemethod").value; 
 	var bookobj =  parent.parent.frames("fileframe").document.all.bookid;
 	var bookid = bookobj.value;
 	 
	if(isNull(paraname)){
		alert("请输入方剂名称！");
		return; 
    }
    if(isNull(jixing)){
		alert("请选择剂型！");
		return; 
    }
    dataProhelper.addPre(paraname,laiyf,jixing,'',symid,eatemethod,bookid,callback4addPre);
}
function callback4addPre(message){ 
	   var cellFunctions = [   
	      function(item) {   
           	return "<input type='radio' name='pre' id='pre' value='"+item.id+"'/>";
          }, 
          function(item) {   
           	return item.prename;
          }, 
          function(item) {  
           	return item.laiyf;
          }, 
          function(item) {  
           	return item.jixing;
          },
          function(item) {  
           	return item.eatemethod;
          },
          function(item) {  
           	return "<a href=javaScript:onclick=deletePre('"+item.id+"')>删除</a><input type=hidden value='"+item.id+"' name='preid_"+item.id + "' id='preid_"+item.id+"/>";
          },
          function(item) {   
           	return "<a href=javaScript:onclick=copyPre('"+item.id+"')>复制</a>";
          }
      ];    
     var index = message.indexOf("$");  
     disid = message.substring(0,index); 
     var rowdata = message.substring(index+1); 
     var data = eval('('+rowdata+')');   
      DWRUtil.addRows("prelist", data, cellFunctions,{
          rowCreator:function(options) { //自定义 tr 的创建行为 
          var trobj = document.createElement("tr");   
          trobj.setAttribute("id","pre_"+disid);  
          return trobj; 
     },cellCreator:function(options) {
	var cell = document.createElement("td"); 
	//if(options.cellNum==0) {//表示第一列，以此类推   
	//  	cell.width = "80%";
	//	}else{
	//	    cell.width = "10%";
	//    cell.align="center";
	     
	//}
	return cell;
}, escapeHtml:false
     });
    alert('方剂信息保存成功！');   
    document.getElementById("prename").value=''; 
 	document.getElementById("laiyf").value=''; 
 	document.getElementById("eatemethod").value=''; 
 	document.getElementById("prename").focus(); 
}


function deletePre(id){  
	if(confirm("确认要删除该方剂及其方剂组成吗？")){
	       dataProhelper.delPre(id,callback4delPre); 
    }
}
function callback4delcheckpre(data){ 
		
}
/*function deletePre(id){  
	if(confirm("确认要删除该方剂吗？")){
	        dataProhelper.queryHerbcout(id,callback4delcheckpre);
    }
}
function callback4delcheckpre(data){
	if("" == data){
		alert("请先删除该方剂相关的中药！");
		return;
	}else{ 
		dataProhelper.delPre(data,callback4delPre);
	} 
}*/
function callback4delPre(data){   
	var tbody  = document.getElementById("prelist");// tbody 的id 
    var rowobj = document.getElementById("pre_"+data);  
    tbody.removeChild(rowobj);
    alert("方剂删除成功！");
}


/**中药组成信息保存*/
var preid = "";
function saveHerbinfo(){
	
 	var paraname = document.getElementById("herbname").value; 
 	var jiliang = document.getElementById("jiliang").value; 
 	var unit = document.getElementById("unit").value; 
 	var preid = getRadioValue("pre");  
 	var bookobj =  parent.parent.frames("fileframe").document.all.bookid;
 	var bookid = bookobj.value;
 	if("undefined" == typeof(preid)){
		alert("请先选择该中药相关的方剂！");
		return;
	}  
	if(isNull(paraname)){
		alert("请输入中药名称！");
		return; 
    }
    if(isNull(jiliang)){
		alert("请输入剂量！");
		return; 
    }
    if(isNull(unit)){
		alert("请输入剂量单位！");
		return; 
    }
    dataProhelper.addHerb(paraname,jiliang,unit,preid,bookid,callback4addHerb);
}
var herbid = "";
function callback4addHerb(message){  
	   var cellFunctions = [    
          function(item) {   
           	return item.herbname;
          },   
          function(item) {  
           	return item.jiliang;
          },
          function(item) {  
           	return item.unit;
          },
          function(item) {  
           	return "<a href=javaScript:onclick=delHerb('"+item.id+"');>删 除</a>";
          }
      ];    
     var index = message.indexOf("$");  
     herbid = message.substring(0,index); 
     var rowdata = message.substring(index+1); 
     var data = eval('('+rowdata+')');   
      DWRUtil.addRows("herblist", data, cellFunctions,{
          rowCreator:function(options) { //自定义 tr 的创建行为 
          var trobj = document.createElement("tr");   
          trobj.setAttribute("id","herb_"+herbid);  
          return trobj; 
     },cellCreator:function(options) {
	var cell = document.createElement("td"); 
	//if(options.cellNum==0) {//表示第一列，以此类推   
	//  	cell.width = "80%";
	//	}else{
	//	    cell.width = "10%";
	//    cell.align="center";
	     
	//}
	return cell;
}, escapeHtml:false
     });
    alert('中药信息保存成功！');   
    document.getElementById("herbname").value=''; 
 	document.getElementById("jiliang").value=''; 
 	document.getElementById("herbname").focus(); 
}
function delHerb(id){ 
	if(confirm("确认要删除该中药吗？")){
    		dataProhelper.delHerb(id,callback4delHerb);
    }
}
function callback4delHerb(data){   
	var tbody  = document.getElementById("herblist");// tbody 的id 
    var rowobj = document.getElementById("herb_"+data);  
    tbody.removeChild(rowobj);
}

/**证候信息保存*/
function saveParainfo(id){
 	var paraname = document.getElementById("paraname").value; 
 	var ancobj =  parent.parent.frames("fileframe").document.all.dis;
 	var bookobj =  parent.parent.frames("fileframe").document.all.bookid;
 	var bookid = bookobj.value;
 	var ancname = "" ;
 	if(ancobj == undefined){
 		alert("请先加工古籍中医病名!");
 		return ;
 	}
 	if(ancobj.length > 1){
 		for(var i=0;i<ancobj.length;i++)
		 {
		     if(ancobj[i].checked){
		     	ancname = ancobj[i].value ;
		     }
		 }
 	}else {
 		if(ancobj.checked){
 			ancname = ancobj.value ;
 		}
 	}
 	if("" == ancname){
 		alert("请先选择该证候对应的古籍中医病名!");
 		return ;
 	}
	if(isNull(paraname)){
		alert("请输入证候名称！");
		return; 
    }
    dataProhelper.addSym(paraname,'',id,bookid,callback4addPara);
}
function callback4addPara(data){ 
	   var index = data.indexOf("$");
       var paraid = data.substring(0,index);
       var paraname = data.substring(index+1);  
       var str = "<span id=para_"+paraid+"><input type=radio onclick=viewpre('"+paraid+"') value="+paraid+" name=sym><font size='5'>" + paraname+"</font>&nbsp;&nbsp;<a href='javaScript:delPara("+paraid+");'>删除</a>&nbsp;&nbsp;</span>";
       var objHtml = document.getElementById("paras");
       var temp = objHtml.innerHTML + str;
       objHtml.innerHTML = temp; 
       alert('证候信息保存成功！');   
       document.getElementById("paraname").value='';
       document.getElementById("paraname").focus(); 
}
function delPara(id){
	if(confirm("确认要删除该证候吗？")){
    		dataProhelper.queryPrecout(id,callback4delcheck);
    }
}
function callback4delcheck(data){
	if("" == data){
		alert("请先删除该证候相关的方剂");
		return; 
	}else{
		dataProhelper.delSym(data,callback4delPara);
	} 
}
function callback4delPara(data){   
	document.getElementById("para_"+data).style.display="none"; 
}

function viewpre(id){ 
    DWRUtil.removeAllRows("prelist");
     DWRUtil.removeAllRows("herblist");
	dataProhelper.queryPreBySymid(id,callback4preview);
}
function callback4preview(data){   
	var dataarr = data.split("#")  
	for(var i = 0;i<dataarr.length;i++){ 
	    var temp = dataarr[i];
	    if("" == temp)
	       continue;
	    addpre(temp);
	}
}

function addpre(message){ 
	   var cellFunctions = [   
	      function(item) {   
           	return "<input type='radio' onclick=viewherb('"+item.id+"') name='pre' id='pre' value='"+item.id+"'/>";
          }, 
          function(item) {   
           	return item.prename;
          }, 
          function(item) {  
            
           	return item.laiyf;
          }, 
          function(item) {  
           	return item.jixing;
          },
          function(item) {  
           	return item.eatemethod;
          },
          function(item) {  
           	return "<a href=javaScript:onclick=deletePre('"+item.id+"')>删除</a><input type=hidden value='"+item.id+"' name='preid_"+item.id + "' id='preid_"+item.id+"/>";
          },
          function(item) {   
           	return "<a href=javaScript:onclick=copyPre('"+item.id+"')>复制</a>";
          }
      ];    
     var index = message.indexOf("$");  
     var disid = message.substring(0,index); 
     var rowdata = message.substring(index+1); 
     var data = eval('('+rowdata+')');   
      DWRUtil.addRows("prelist", data, cellFunctions,{
          rowCreator:function(options) { //自定义 tr 的创建行为 
          var trobj = document.createElement("tr");   
          trobj.setAttribute("id","pre_"+disid);  
          return trobj; 
     },cellCreator:function(options) {
	var cell = document.createElement("td"); 
	//if(options.cellNum==0) {//表示第一列，以此类推   
	//  	cell.width = "80%";
	//	}else{
	//	    cell.width = "10%";
	//    cell.align="center";
	     
	//}
	return cell;
}, escapeHtml:false
     }); 
      
      
}
//复制方剂
function copyPre(preid){ 
     var symid = document.getElementById("mudisym").value;
     if("0" == symid){
        alert("请选择要方剂复制的目的证候！");
        return;
     }
     dataProhelper.copyPreAndHerb(preid,symid,callback4Copy);
}
function callback4Copy(data){

 alert("方剂复制成功!");
}
///
function viewherb(id){
    DWRUtil.removeAllRows("herblist");
	dataProhelper.queryHerbByPreid(id,callback4herbview);
}
function callback4herbview(data){   
	var dataarr = data.split("#") ;
	for(var i = 0;i<dataarr.length;i++){ 
	    var temp = dataarr[i];
	    if("" == temp)
	       continue;
	    addherb(temp);
	}
}

function addherb(message){ 
	   var cellFunctions = [    
          function(item) {   
           	return item.herbname;
          },   
          function(item) {  
           	return item.jiliang;
          },
          function(item) {  
           	return item.unit;
          },
          function(item) {  
           	return "<a href=javaScript:delHerb('"+item.id+"');>删 除</a>";
          }
      ];    
     var index = message.indexOf("$");  
     herbid = message.substring(0,index); 
     var rowdata = message.substring(index+1); 
     var data = eval('('+rowdata+')');   
      DWRUtil.addRows("herblist", data, cellFunctions,{
          rowCreator:function(options) { //自定义 tr 的创建行为 
          var trobj = document.createElement("tr");   
          trobj.setAttribute("id","herb_"+herbid);  
          return trobj; 
     },cellCreator:function(options) {
	var cell = document.createElement("td"); 
	//if(options.cellNum==0) {//表示第一列，以此类推   
	//  	cell.width = "80%";
	//	}else{
	//	    cell.width = "10%";
	//    cell.align="center";
	     
	//}
	return cell;
}, escapeHtml:false
     });
}



</script>
</head>
<body>
 
<div class="deta_proce" style="border-top:none;margin-left:0"> 
    <div>
        <table border="0" cellspacing="0" cellpadding="0"  class="atta_tab">
          <caption>证候信息</caption> 
          <tr> 
           <td width="35%">
           <table border="0" cellspacing="0" cellpadding="0"  > 
          <tr>
            <td width="30%">证候名称：</td> 
            <td><input type="text" name="paraname" id="paraname" style="width:99%"/></td>
            <td width="10%"><input onclick="saveParainfo('<%=disobj==null?"":disobj.getId() %>')" type="button" value="保存" class="but_3"/></td>
          </tr>  
        </table> 
           </td>
           <td id="paras">
           <%
            if(null != disobj && !(disobj.getSymset().isEmpty())){  
                    Iterator paraiter = disobj.getSymset().iterator(); 
                   while(paraiter.hasNext()){ 
          	             DnmsSym para = (DnmsSym)paraiter.next(); 
          	             Long pid = para.getId(); 
          	             out.print("<span id=para_"+pid+"><input type=radio onclick=viewpre('"+pid+"') value="+pid+" name=sym><font size='5'>"+para.getSymptomname()+"</font>");
          	             out.print("<a href=javaScript:delPara('"+pid+"');>");
          	             out.print(" 删 除 </a></span>"); 
		          	    symstr.append("<option value='");
		          	    symstr.append(pid);
		          	    symstr.append("'>");
		          	    symstr.append(para.getSymptomname());
		          	    symstr.append("</option>");
		          	    
		          	    
           }}else{ 
            	 List disobjList = (List)request.getAttribute("disobjList"); 
            	 if(null != disobjList){
            	 	for (int j=0; j<disobjList.size(); j++){
	           		 	DnmsDisease dnmsdis = (DnmsDisease)disobjList.get(j);
	           		 	Iterator dnmsiter = dnmsdis.getSymset().iterator();
	           		 	while(dnmsiter.hasNext()){ 
	           		 		DnmsSym para = (DnmsSym)dnmsiter.next(); 
	          	             Long pid = para.getId(); 
	          	             out.print("<span id=para_"+pid+"><input type=radio onclick=viewpre('"+pid+"') value="+pid+" name=sym><font size='5'>"+para.getSymptomname()+"</font>");
	          	             out.print("<a href=javaScript:delPara('"+pid+"');>");
	          	             out.print(" 删 除 </a></span>"); 
			          	    symstr.append("<option value='");
			          	    symstr.append(pid);
			          	    symstr.append("'>");
			          	    symstr.append(para.getSymptomname());
			          	    symstr.append("</option>");
	           		 	}
           		 	}
            	 }
           		 
              } %> 
           </td> 
           <td width="15%">
            目的证候
           <select name="mudisym" id="mudisym" style="width:100%">
            <option value="0">--请选择--</option>
           <%=symstr.toString() %>
           </select>
           </td>
	          </tr> 
        </table>
         
        <table border="0" cellspacing="0" cellpadding="0" class="atta_tab">
          <caption>方剂信息</caption>  
          <tr> 
            <th width="8%">选择</th>
            <th width="15%">方名</th>
            <th width="25%">来源方</th>
            <th width="10%">剂型</th>
            <th width="30%">煎服药方法</th>
            <th width="6%">删除</th>
            <th width="6%">复制</th> 
          </tr>
          <tbody id=prelist> 
           </tbody>
        </table>
         <table border="0" cellspacing="0" cellpadding="0"> 
          <tr>
            <td width="10%">方名：</td> 
            <td width="15%"><input type="text" name="prename" id="prename" style="width:99%"/></td>
            <td width="10%">来源方</td>
            <td width="15%"><input type="text" name="laiyf" id="laiyf" style="width:99%"/></td>
            <td width="10%">剂型</td>
            <td width="10%"><%out.println(DictCacheUtil.getInst().selectStrNodefault("jixing","width:100%","","9"));%></td>
            <td width="10%">煎服方法</td>
            <td width="15%"><input type="text" name="eatemethod" id="eatemethod" style="width:99%"/></td>
            <td width="5%"><input onclick="savePreinfo()" type="button" value=" 保 存 " class="but_2"/></td>
          </tr>  
        </table> 
        <table border="0" cellspacing="0" cellpadding="0" class="atta_tab">
          <caption>中药组成</caption>  
          <tr>  
            <th width="25%">名称</th>
            <th width="25%">剂量</th>
            <th width="25%">单位</th>
            <th width="10%">删除</th> 
          </tr>
          <tbody id=herblist>
           
           </tbody>
        </table>
         <table border="0" cellspacing="0" cellpadding="0" class="atta_tab"> 
          <tr>
            <td width="10%">名称：</td> 
            <td width="25%"><input type="text" name="herbname" id="herbname" style="width:99%"/></td>
            <td width="10%">剂量：</td>
            <td width="25%"><input type="text"   name="jiliang" id="jiliang" style="width:99%"/></td>
            <td width="10%">单位：</td>
            <td width="10%"><%out.println(DictCacheUtil.getInst().selectStrNodefault("unit","width:100%","","11"));%></td>
            <td width="10%"><input onclick="saveHerbinfo()" type="button" value=" 保 存 " class="but_2"/></td>
          </tr>  
        </table> 
    </div>
</div>
</body>
</html>

